package com.wnhoo.smartfishtank.util;


import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by kevinwen on 2014/11/26.
 * Copyright (c) 2014/11/26.KevinWen. All rights reserved.
 */
public class SNWifiManage {

    private String tag = "SNWifiManage";
    private WifiManager wifiManager;// 声明管理对象
    private WifiInfo wifiInfo;// Wifi信息
    private List<ScanResult> scanResultList; // 扫描出来的网络连接列表
    private List<WifiConfiguration> wifiConfigList;// 网络配置列表


    public SNWifiManage(Context context) {
        this.wifiManager = (WifiManager) context
                .getSystemService(Context.WIFI_SERVICE);// 获取Wifi服务

        // 得到Wifi信息
        this.wifiInfo = wifiManager.getConnectionInfo();// 得到连接信息
        this.openWifi();
    }

    // 打开/关闭 wifi
    public boolean openWifi() {
        if (!this.getWifiStatus()) {
            return wifiManager.setWifiEnabled(true);
        } else {
            return false;
        }

    }

    // 扫描网络
    public void startScan() {
        wifiManager.startScan();

        scanResultList = wifiManager.getScanResults(); // 扫描返回结果列表

        wifiConfigList = wifiManager.getConfiguredNetworks(); // 扫描配置列表
    }



    // 获取扫描列表
    public StringBuilder lookUpscan() {
        StringBuilder scanBuilder = new StringBuilder();

        for (int i = 0; i < scanResultList.size(); i++) {
            scanBuilder.append("编号" + (i + 1)+":");
            scanBuilder.append(scanResultList.get(i).toString());  //所有信息
            scanBuilder.append("\n");
        }

        return scanBuilder;
    }


    // 返回当前连接的网络的ID
    public int getCurrentNetId() {
        return (wifiInfo == null) ? null : wifiInfo.getNetworkId();
    }

    public boolean getWifiStatus()
    {
        return wifiManager.isWifiEnabled();
    }

    public List<ScanResult> getWifiList() {
        return scanResultList;
    }

    public List<WifiConfiguration> getWifiConfigList() {
        return wifiConfigList;
    }

    public String getSSID() {
        return (wifiInfo == null) ? null : wifiInfo.getSSID();
    }

    // 返回当前wifi所有信息
    public String getwifiInfo() {
        return (wifiInfo == null) ? null : wifiInfo.toString();

        //getwifiInfo:SSID: SCINAN, BSSID: 0c:72:2c:a8:7b:28, MAC: 00:02:72:bc:70:5f, Supplicant state: COMPLETED, RSSI: -50, Link speed: 150, Net ID: 8, Explicit connect: false
    }

    public String[] getAllSSID() {

        //Log.d(tag,lookUpscan().toString());
        String[] SSIDArray;
        if(wifiInfo==null||scanResultList==null) {
            //Log.d(tag,"wifiInfo==null||scanResultList==null");
            SSIDArray = new String[1];
            SSIDArray[0] = "自动获取显示路由器SSID";

        }else {
            //Log.d(tag,"wifiInfo!=null");
            //           编号1:SSID: SCINAN, BSSID: 0c:72:2c:a8:7b:28, capabilities: [WPA-PSK-CCMP][WPA2-PSK-CCMP][WPS][ESS], level: -49, frequency: 2437
            //           编号2:SSID: Tenda_54E0D8, BSSID: c8:3a:35:54:e0:d8, capabilities: [WPA-PSK-CCMP][ESS], level: -71, frequency: 2437
            //Log.d(tag,"wifiInfo="+wifiInfo.toString());
            //Log.d(tag,"搜索到的SSID有："+scanResultList.size()+"个");
            SSIDArray =new String[scanResultList.size()];
            //            SSIDArray =new String[scanResultList.size()];

            for (int i = 0; i < scanResultList.size(); i++) {

                try {


                    String temp = scanResultList.get(i).toString();
                    //                temp.startsWith("scinan");//字符串是否以scinan开始
                    //Log.d(tag,"scanResultList.get(i)="+temp);
                    int start = temp.indexOf(":")+2;
                    int end = temp.indexOf(",");
                    SSIDArray[i] = temp.substring(start,end);
                    //Log.d(tag,"!!!SSIDArray["+i+"]="+SSIDArray[i]+"!!!");
                } catch (Exception e) {
                    // TODO: handle exception
                    //Log.d(tag,"getAllSSID"+e.toString());
                }
            }
        }
        return SSIDArray ;
    }




    public String[] getAllRouterSSID() {

        String[] SSIDArray;
        if(wifiInfo==null||scanResultList==null) {
            //Log.d(tag,"wifiInfo==null||scanResultList==null");
            SSIDArray = new String[1];
            SSIDArray[0] = "No wifi information";
            return SSIDArray;
        }else {
            ArrayList<String> router_list = new ArrayList<String>();
            for(ScanResult tempResult :scanResultList){

                try {
                    String temp = tempResult.toString();
                    int start = temp.indexOf(":")+2;
                    int end = temp.indexOf(",");
                    temp = temp.substring(start,end);
                    if(temp.indexOf("SN_")==-1&&temp.indexOf("SCINANSOCKET")==-1){

                        router_list.add(temp);
                    }
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }
            SSIDArray =new String[router_list.size()];

            for (int i = 0; i <router_list.size(); i++) {

                try {

                    SSIDArray[i] = router_list.get(i);
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }
            return SSIDArray ;
        }
    }



}